using System;
using System.Collections.Generic;
using System.Text;

namespace Eve.Data
{
    public class Operation : EveProperty
    {
        private int activityId;
        private string operationName;
        private string description;
        private int fringe;
        private int corridor;
        private int hub;
        private int border;
        private int ratio;
        private int caldariStationTypeId;
        private int minmatarStationTypeId;
        private int amarrStationTypeId;
        private int gallenteStationTypeId;
        private int joveStationTypeId;

        private CorporateActivity activity;
        private StationType caldariStationType;
        private StationType minmatarStationType;
        private StationType amarrStationType;
        private StationType gallenteStationType;
        private StationType joveStationType;
        private List<Service> services;

        public CorporateActivity Activity
        {
            get
            {
                if (activity == null)
                    activity = new CorporateActivity(db, activityId);
                return activity;
            }
        }
        public string Name
        {
            get { return operationName; }
        }
        public string Description
        {
            get { return description; } 
        }
        public int Fringe
        {
            get { return fringe; }
        }
        public int Corridor
        {
            get { return corridor; }
        }
        public int Hub
        {
            get { return hub; }
        }
        public int Ratio
        {
            get { return ratio; }
        }
        public StationType CaldariStationType
        {
            get
            {
                if (caldariStationType == null)
                    caldariStationType = new StationType(db, caldariStationTypeId);
                return caldariStationType;
            }
        }
        public StationType MinmatarStationType
        {
            get
            {
                if (minmatarStationType == null)
                    minmatarStationType = new StationType(db, minmatarStationTypeId);
                return minmatarStationType;
            }
        }
        public StationType AmarrStationType
        {
            get
            {
                if (amarrStationType == null)
                    amarrStationType = new StationType(db, amarrStationTypeId);
                return amarrStationType;
            }
        }
        public StationType GallenteStationType
        {
            get
            {
                if (gallenteStationType == null)
                    gallenteStationType = new StationType(db, gallenteStationTypeId);
                return gallenteStationType;
            }
        }
        public StationType JoveStationType
        {
            get
            {
                if (joveStationType == null)
                    joveStationType = new StationType(db, joveStationTypeId);
                return joveStationType;
            }
        }
        public List<Service> Services
        {
            get
            {
                if (services == null)
                {
                    services = new List<Service>();
                    string query = "SELECT staServices.* FROM staServices, staOperationServices WHERE staServices.serviceID=staOperationServices.serviceID AND staOperationServices.operationID=" + Id;
                    foreach (DbDataRow row in db.ExecuteQuery(query))
                        services.Add(new Service(row));
                }
                return services; 
            }
        }

        internal Operation(DbDataRow dr)
            : base(dr.Database, EvePropertyType.Operation, dr["operationID"])
        {
            Fill(dr);
        }
        internal Operation(Database db, int id)
            : base(db, EvePropertyType.Operation, id)
        {
            Fill(db.ExecuteSingle("SELECT * FROM staOperations WHERE operationID=" + id));
        }

        private void Fill(DbDataRow dr)
        {
            activityId = dr["activityID"];
            operationName = dr["operationName"];
            description = dr["description"];
            fringe = dr["fringe"];
            corridor = dr["corridor"];
            hub = dr["hub"];
            border = dr["border"];
            ratio = dr["ratio"];
            caldariStationTypeId = dr["caldariStationTypeID"];
            minmatarStationTypeId = dr["minmatarStationTypeID"];
            amarrStationTypeId = dr["amarrStationTypeID"];
            gallenteStationTypeId = dr["gallenteStationTypeID"];
            joveStationTypeId = dr["joveStationTypeID"];
        }
    }
}
